﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ihongma.Common;
namespace ihongma.Web.admin.Exchange
{
    public partial class Exchange_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.Exchange().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("Exchange", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.Exchange bll = new BLL.Exchange();
            Model.Exchange model = bll.GetModel(_id);

         
          
            model.number = int.Parse(txtnumber.Text);

            if (ddlcategory_id.Value == "0")
            {
                model.price = decimal.Parse(this.txtprice.Text);
            }
            else
            {
                model.discount = decimal.Parse(this.txtprice.Text);
            }
            model.content = txtcontent.Text;
            model.StartDateTime = DateTime.Parse(texStartDateTime.Text);
            model.EndDateTime = DateTime.Parse(texEndDateTime.Text);
          
            model.ExchangePerson = GetAdminInfo().user_name;
            model.category_id = int.Parse(ddlcategory_id.Value);
            model.field1 = field1.Text;
            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改礼品卡:" + model.id); //记录日志
                result = true;
            }

            return result;
        }
        #endregion
        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.Exchange bll = new BLL.Exchange();
            Model.Exchange model = bll.GetModel(_id);
            ddlcategory_id.Value = model.category_id.ToString();
            texStartDateTime.Text = model.StartDateTime.ToString();
            texEndDateTime.Text = model.EndDateTime.ToString();
            txtnumber.Text = model.number.ToString();
            txtnumber.Enabled = false;
            txtcontent.Text = model.content;
            field1.Text = model.field1;
            if (model.category_id == 0)
            {
                this.txtprice.Text = model.price.ToString();
            }
            else
            {
                this.txtprice.Text = model.discount.ToString();
            }
           
        }
        #endregion
        #region 增加操作=================================
        private bool DoAdd()
        {
            bool ret = false;
            BLL.Exchange bll = new BLL.Exchange();
            try
            {


                for (int i = 0; i < int.Parse(txtnumber.Text); i++)
                {
                    //string SN = Utils.GetCheckCode(8, true);
                    String SN = "";
                    if (!this.txt_sn.Text.Equals(""))
                    {
                        SN = this.txt_sn.Text.ToUpper();
                    }
                    else
                    {
                        SN = Utils.GetCheckCode(8, true);
                    }
                    while (bll.Exists(SN))
                    {
                        SN = Utils.GetCheckCode(8, true);
                    }
                    Model.Exchange model = new Model.Exchange();

                    model.SN = SN;

                    // model.SN = Utils.GetRnd(6,true,true,true,false,"");
                    model.number = 0;
                    model.CreatedDateTime = DateTime.Now;
                    if (ddlcategory_id.Value == "0")
                    {
                        model.price = decimal.Parse(this.txtprice.Text);
                    }
                    else
                    {
                        model.discount = decimal.Parse(this.txtprice.Text);
                    }
                    if (ddlcategory_id.Value == "4")
                    {
                        model.content = "公司优惠";
                    }
                    else
                    {
                        model.content = txtcontent.Text;
                    }
                    model.StartDateTime = DateTime.Parse(texStartDateTime.Text);
                    model.EndDateTime = DateTime.Parse(texEndDateTime.Text);
                    model.UsedStatus = true;
                    model.ExchangePerson = GetAdminInfo().user_name;
                    model.category_id = int.Parse(ddlcategory_id.Value);
                    model.field1 = DateTime.Now.ToShortDateString() + field1.Text;
                    bll.Add(model);
                }
            AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加生成卡成功:" + int.Parse(txtnumber.Text)); //记录日志
            ret = true;
            }
            catch (Exception)
            {

                throw;
            }
            return ret;
            
        }
        #endregion
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("Exchange", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("生成礼品卡发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("礼品卡修改成功！", "Exchange_list.aspx", "Success");
            }
            else //添加
            {
                ChkAdminLevel("Exchange", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("生成礼品卡过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("生成礼品卡成功！", "Exchange_list.aspx", "Success");
            }
        }
        
    }
}